/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: sageSync.h
 * Author : Byungil Jeong, Rajvikram Singh, 
 * Description : This file contains the class which provides the syncing
 *               mechanism for keeping all the sage processes in sync. 
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/

#ifndef _SAGESYNC_H
#define _SAGESYNC_H

#include "sageBase.h" 

// define the sync messages and the acknowledgements. 
// These are randomly chosen numbers
#define SAGE_SYNC_MSG_LEN			16
#define MAX_NO_OF_SYNC_SLAVES		500

class syncSlaveData {
public:
	int 				clientSockFd;
	sockaddr_in		clientAddr;
	int				frame;
	
	syncSlaveData() : frame(0) {}
};

//forward declarations
class sageSyncClient;

class sageSyncServer {
private:
	int					serverSockFd;
	sockaddr_in			serverAddr;
	syncSlaveData 		syncSlaves[MAX_NO_OF_SYNC_SLAVES];
	pthread_t			syncThreadID;
	
	static void* 		syncServerThread(void*);
	int	noOfSyncSlaves, noOfUpdates, curFrame;
		
 public:
	sageSyncServer() : noOfSyncSlaves(0), noOfUpdates(0), curFrame(-1) {}
	~sageSyncServer();
		
	bool init(int port, int startFrame);
	int checkUpdates();
	int waitForUpdates();
	int waitForUpdates(int slaveNum);
	int sendSync();
	int checkSlaves();
	int distributeSync(char *data);
	void killAllClients();		/// closes all the open client sockets
	
}; // End of sageSyncServer

class sageSyncClient {
private:
	int clientSockFd;
	bool blocked;

public:
	sageSyncClient();
	int connectToServer(char*, int);
	~sageSyncClient();

	int sendSlaveUpdate(int);
	int waitForSync(char* &data);
	bool checkSync(int &frame);
	bool isBlocked() { return blocked; }
}; // End of tvSyncClient

// class for synchronization among master and slave threads
class sageThreadSync {
private:
	pthread_mutex_t	*slaveLock;
	pthread_mutex_t	*masterLock;
	int slaveNum;

public:
	sageThreadSync(int sNum = 1);
	~sageThreadSync();
	int synchronize(int rank = 0);
	int resynchronize(int rank = 0);
};

#endif
